/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.interceptor;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor;
import org.springframework.lang.Nullable;

/**
 * Advisor driven by a {@link TransactionAttributeSource}, used to include
 * a transaction advice bean for methods that are transactional.
 *
 * @author Juergen Hoeller
 * @since 2.5.5
 * @see #setAdviceBeanName
 * @see TransactionInterceptor
 * @see TransactionAttributeSourceAdvisor
 */
/*

1. 总结
BeanFactoryTransactionAttributeSourceAdvisor 是 Spring 事务管理框架中的一个具体实现类，继承自 AbstractBeanFactoryPointcutAdvisor。
它通过 TransactionAttributeSource 来确定哪些方法需要事务处理，并将这些方法的事务逻辑委托给配置的事务增强器（Advice）。
与 TransactionAttributeSourceAdvisor 相比，它更依赖于 BeanFactory 的上下文，支持延迟初始化和 BeanFactory 管理的 Advice。
2. 内部成员说明
transactionAttributeSource: 类型为 TransactionAttributeSource，用于获取方法的事务属性。
pointcut: 类型为 TransactionAttributeSourcePointcut，内部类，负责根据 transactionAttributeSource 判断方法是否需要事务处理。
setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource): 设置事务属性源。
setClassFilter(ClassFilter classFilter): 设置类过滤器，用于限定哪些类的方法需要被检查。
getPointcut(): 返回当前的 Pointcut 实例。
3. 类关系结构
Advisor (interface)
└── PointcutAdvisor (interface)
    └── AbstractPointcutAdvisor (abstract class)
        ├── TransactionAttributeSourceAdvisor (class)	不直接依赖 BeanFactory，通常在非容器环境下使用
        └── AbstractBeanFactoryPointcutAdvisor (abstract class)	支持 BeanFactory 上下文，可以延迟初始化 Advice
            └── BeanFactoryTransactionAttributeSourceAdvisor (class) 适用于 Spring 容器环境，配合 TransactionInterceptor 使用

5. 使用场景
在 Spring 容器中配置事务拦截器时，使用 BeanFactoryTransactionAttributeSourceAdvisor。
当需要将事务逻辑与业务逻辑分离，并通过 BeanFactory 管理事务增强器时。
在需要延迟初始化事务增强器以提高性能的场景中。
 */
@SuppressWarnings("serial")
public class BeanFactoryTransactionAttributeSourceAdvisor extends AbstractBeanFactoryPointcutAdvisor {

	@Nullable
	private TransactionAttributeSource transactionAttributeSource;

	private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() {
		@Override
		@Nullable
		protected TransactionAttributeSource getTransactionAttributeSource() {
			return transactionAttributeSource;
		}
	};


	/**
	 * Set the transaction attribute source which is used to find transaction
	 * attributes. This should usually be identical to the source reference
	 * set on the transaction interceptor itself.
	 * @see TransactionInterceptor#setTransactionAttributeSource
	 */
	public void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource) {
		this.transactionAttributeSource = transactionAttributeSource;
	}

	/**
	 * Set the {@link ClassFilter} to use for this pointcut.
	 * Default is {@link ClassFilter#TRUE}.
	 */
	public void setClassFilter(ClassFilter classFilter) {
		this.pointcut.setClassFilter(classFilter);
	}

	@Override
	public Pointcut getPointcut() {
		return this.pointcut;
	}

}
